package com.aixin.project.servlet;

import com.aixin.project.data.AxSession;
import com.aixin.project.data.CommonConstants;
import com.aixin.project.data.Course;
import com.aixin.project.data.CourseStatus;
import com.aixin.project.data.Expert;
import com.aixin.project.data.UserType;
import com.aixin.project.handler.CourseHandler;
import com.aixin.project.handler.ExpertHandler;
import org.apache.velocity.Template;
import org.apache.velocity.context.Context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA. User: Administrator Date: 12-4-13 Time: 下午11:45 To
 * change this template use File | Settings | File Templates.
 */
public class IndexServlet extends BaseServlet {
    @Override
    protected Template execute(HttpServletRequest request,
                               HttpServletResponse response, Context ctx) {
        AxSession axsession = (AxSession) request.getSession().getAttribute(CommonConstants.USER_SESSION);
        List<Course> courses = CourseHandler.listComingCourse();
        if (null != courses && courses.size() >=2 )
            courses = courses.subList(0, 2);
        ctx.put("course", courses);

        List<Expert> experts = ExpertHandler.hallexpertList();
        if (null != experts && experts.size() >= 2)
            experts = experts.subList(0, 2);
        System.out.println("expert size:" + experts == null ? 0 : experts.size());
        ctx.put("experts", experts);

        if(null != axsession){
        	if(UserType.USER == axsession.getUserType()){
        		Map<String,Integer> map = CourseHandler.courseStatusCount(axsession.getUid());
            	request.setAttribute("wks", map.get("wks"));
            	request.setAttribute("zbz", map.get("zbz"));
            	request.setAttribute("wzf", map.get("wzf"));
            	request.setAttribute("u", "1");
        	}else{
        		HashMap<String, Integer> courseStatusMap = CourseHandler.courseStatusByEid(axsession.getUid());
        		int nostarCount = (null == courseStatusMap || null == courseStatusMap.get(CourseStatus.NOSTART.toString()))?0:courseStatusMap.get(CourseStatus.NOSTART.toString());
                int liveCount = (null == courseStatusMap || null == courseStatusMap.get(CourseStatus.LIVE.toString()))?0:courseStatusMap.get(CourseStatus.LIVE.toString());
                request.setAttribute("yj", liveCount);
                request.setAttribute("nst", nostarCount);
                request.setAttribute("u", "0");
        	}
        	
        }
/*        if (null == axsession) {
            request.setAttribute("navindex", true);
            return getTemplate("index.vm");
        }

        UserType type = axsession.getUserType();
        if (type.equals(UserType.USER)) {
            request.setAttribute("navindex", true);
            return getTemplate("index.vm");
        } else if (type.equals(UserType.EXPERT)) {
            this.sendRedirect(response, "/course");
            return null;
        } else*/
        request.setAttribute("navindex", true);
        return getTemplate("index.vm");
    }
}
